package com.wsc.wisdomchina.activitity.wisdomParty;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wsc.wisdomchina.R;
import com.wsc.wisdomchina.Tools.JsonTools;
import com.wsc.wisdomchina.Tools.Tools;
import com.wsc.wisdomchina.activitity.ExaminationActivity;
import com.wsc.wisdomchina.application.MyApplication;
import com.wsc.wisdomchina.callback.ContributorView;
import com.wsc.wisdomchina.javaBean.RespObj;
import com.wsc.wisdomchina.javaBean.wisdomParty.PartyLineTestBean;
import com.wsc.wisdomchina.presenter.ContributorPresenter;

import java.util.Date;

public class PartyStartTestActivity extends Activity implements View.OnClickListener, ContributorView {
    private ImageView regist_back;
    private TextView tv_headtitle;
    private Context context;
    private MyApplication myApplication;
    private String pmcId;//考核Id
    private String memId;//人员Id
    private String streetId;//人员Id
    private ContributorPresenter contributorPresenter;
    private final String FRAG = "PartyStartTestActivity";
    private Button startTest;
    private ImageView headImge;//考核人员照片
    private TextView name;//考核人姓名
    private TextView name_time;//考核名称和时间
    private TextView test_standard;//答题标准
    private TextView qualified_standard;//合格标准
    private PartyLineTestBean bean =null;//考生信息

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_party_start_test);
        context = this;
        myApplication = (MyApplication) getApplication();
        pmcId = getIntent().getStringExtra("pmcId");
        memId = getIntent().getStringExtra("memId");
        streetId = getIntent().getStringExtra("streetId");
        contributorPresenter = new ContributorPresenter(this, context);
        showData();
        initView();
    }

    private void showData() {
        contributorPresenter.getOnlineExamOne(pmcId, memId, FRAG);
    }

    private void initView() {
        regist_back = (ImageView) findViewById(R.id.regist_back);
        regist_back.setOnClickListener(this);
        tv_headtitle = (TextView) findViewById(R.id.tv_headtitle);
        tv_headtitle.setText("在线答题");

        startTest = ((Button) findViewById(R.id.start_test));
        startTest.setOnClickListener(this);

        headImge = ((ImageView) findViewById(R.id.info_headImg));
        name = ((TextView) findViewById(R.id.info_name));

        name_time = ((TextView) findViewById(R.id.info_name_time));
        test_standard = ((TextView) findViewById(R.id.info_test_standard));
        qualified_standard = ((TextView) findViewById(R.id.info_qualified_standard));
    }

    private void setData(PartyLineTestBean data) {
        bean = data;
        name.setText(data.getmName());
        Glide.with(context).load(data.getHeadImg()).into(headImge);
        name_time.setText(data.getCheckName() + " " + Tools.formatDateTimeyyyyMMDD(new Date(data.getCreateTime()), "yyyy.MM.dd"));
        test_standard.setText(data.getTopicLength() + "题," + data.getCheckTimeLimit() + "分钟");
        qualified_standard.setText("答对" + data.getSuccessCriteria() + "以上");
    }

    @Override
    public void onLoadContributorStart(String flag) {

    }

    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try {
            if (flag.equals(FRAG)) {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object object = respObj.getMessage();
                if (code.equals("200") && object != null) {
                    String jsonMessage = JsonTools.objectToJsonString(object);
                    PartyLineTestBean lineTestBean = JsonTools.jsonStringToObject(jsonMessage, PartyLineTestBean.class);
                    if (lineTestBean != null) {
                        setData(lineTestBean);
                    }
                }
            }
        } catch (Exception e) {

        }

    }

    @Override
    public void onLoadContributorComplete(String flag) {

    }

    @Override
    public void onLoadContributorError(String error, String flag) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.start_test:
                if (bean != null) {
                    Intent intent = new Intent(this, ExaminationActivity.class);
                    intent.putExtra("pmcId", pmcId);
                    intent.putExtra("time", bean.getCheckTimeLimit());
                    intent.putExtra("checkId", bean.getId());
                    intent.putExtra("memId", memId);
                    intent.putExtra("streetId", streetId);
                    intent.putExtra("mName", bean.getmName());
                    startActivity(intent);
                }
                break;
            case R.id.regist_back:
                finish();
                break;
        }
    }
}
